package org.zyj.io.example.case8;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.nio.charset.StandardCharsets;
import java.util.List;

class ClientHandler extends SimpleChannelInboundHandler<Long> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        var list = List.of(123L, 321L, 999L, 888L, "a");
        for (Object lo : list) {
            if (lo instanceof CharSequence ss) {
                ctx.writeAndFlush(Unpooled.copiedBuffer(ss, StandardCharsets.UTF_8));
            } else {
                System.out.println("向服务端发送数据:" + lo);
                ctx.writeAndFlush(lo);
            }
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Long msg) throws Exception {
        System.out.println("接收到服务器发送信息: " + msg);
    }
}
